package com.ecgsystem.thread;

import java.io.IOException;
import java.net.ConnectException;
import java.util.StringTokenizer;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.PropertyInfo;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapPrimitive;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import org.xmlpull.v1.XmlPullParserException;

import com.ecgsystem.activity.MainActivity;
import com.ecgsystem.activity.OfflineECG;
import com.ecgsystem.constants.Constants;



import android.util.Log;

/**
 * TODO Put here a description of what this class does.
 *
 * @author ANHTUAN.
 *         Created Sep 28, 2012.
 */
public class ThreadGetSignalByFrame extends Thread implements Constants {
	private String recordID;
	private long offset;
	private boolean firsttime;
	private SoapObject request;
	private SoapPrimitive response;
	private String resultStr;
	private HttpTransportSE transport;
	private SoapSerializationEnvelope envelope;
	private PropertyInfo dirProperty;
	public static boolean hasRecord[] = { false, false, false, false, false, false,
			false, false, false, false, false, false }; // danh dau dao
														// trinh nao duoc do
	private String end;

	/**
	 * TODO Put here a description of what this method does.
	 *
	 * @return String
	 */
	public String getRecordID() {
		return this.recordID;
	}

	private boolean _run;

	/**
	 * TODO Put here a description of what this method does.
	 *
	 * @param recordID
	 */
	public void setRecordID(String recordID) {
		this.recordID = recordID;
	}

	/**
	 * TODO Put here a description of what this constructor does.
	 *
	 * @param recordID
	 */
	public ThreadGetSignalByFrame(String recordID) {
		this.recordID = recordID.trim();
		this.offset = 0;
		this.firsttime = true;

		this.response = null;
		this.dirProperty = new PropertyInfo();
		this.dirProperty.setName("recordDir");
		this.dirProperty.setValue(MainActivity.conn.getDirOfRecord(recordID));
		this.dirProperty.setType(String.class);

		this.envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
		this.envelope.dotNet = true;

		this._run = true;

		this.transport = new HttpTransportSE(URL);

	}

	@Override
	public void run() {
		while (this._run) {
			this.request = new SoapObject(NAMESPACE, METHOD_NAME6);
			this.request.addProperty(this.dirProperty);

			PropertyInfo osProperty = new PropertyInfo();
			osProperty.setName("offset");
			osProperty.setValue(this.offset);
			osProperty.setType(Long.class);
			this.request.addProperty(osProperty);

			this.envelope.setOutputSoapObject(this.request);

			try {
				this.transport.call(SOAP_ACTION6, this.envelope);
				this.response = (SoapPrimitive) this.envelope.getResponse();
				this.resultStr = this.response.toString();

				Log.i("receivedSignal", this.resultStr);
				StringTokenizer st = new StringTokenizer(this.resultStr, "\t");

				// Kiem tra ket thuc hay chua
				this.end = st.nextToken();

				this.offset = Long.valueOf(st.nextToken());
				// Doc 12 tin hieu dau de xem dao trinh nao duoc do
				if (this.firsttime) {
					for (int i = 0; i < 12; i++) {
						if (st.nextToken().equals("1")) {
							ThreadGetSignalByFrame.hasRecord[i] = true;
						} else {
							OfflineECG.QueueDraw[i] = null;
						}
					}
					this.firsttime = false;
					//DynamicGraphActivity.startDraw = true;
				}
				while (st.hasMoreTokens()) {
					st.nextToken(); // bo qua thoi gian

					for (int i = 0; i < 12; i++) {
						Float signal = Float.valueOf(st.nextToken());
						if (ThreadGetSignalByFrame.hasRecord[i]) {
							OfflineECG.QueueDraw[i].add(signal);
						}
					}
				}
				OfflineECG.incrementLoadedPercent(25);
				if (this.end.equals("end")) {
					this._run = false;
					OfflineECG.startDraw = true;
					//DynamicGraphActivity._run = false;
					//StoredSignal.setLoadedPercent(100);
					//DynamicGraphActivity.auto_draw = true;
					//DynamicGraphActivity.incrementLoadedPercent(25);
				} else {
					//DynamicGraphActivity.incrementLoadedPercent(25);
				}
			} catch (NumberFormatException e) {
				OfflineECG._run = false;
				OfflineECG.startDraw = true;
				OfflineECG.displayAlert("Khuân dạng bản ghi không đúng.");
				e.printStackTrace();
			} catch (XmlPullParserException e) {
				e.printStackTrace();
			} catch(ConnectException e){
				OfflineECG._run = false;
				OfflineECG.startDraw = true;
				OfflineECG.displayAlert("Kết nối mạng quá chậm!");
				e.printStackTrace();
			}
			catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
	
	public void shutdown(){
		this._run = false;
	}
}
